package com.woniuxy.controller;

import com.github.pagehelper.util.StringUtil;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacManagerService;
import com.woniuxy.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    RbacManagerService rbacManagerService;

    @RequestMapping("/login")
    public ResponseEntity login(String username, String password) {
//先不去访问数据直接生产token
        /*String sign = JWTUtils.sign(username, password);
        return new ResponseEntity<>(200, "登陆成功", sign);*/
        RbacManager rbacManager = rbacManagerService.login(username, password);
        if (!StringUtils.isEmpty(rbacManager)) {
            String token = JWTUtils.sign(username, password);
            rbacManager.setToken(token);
            return new ResponseEntity<>(200, "登录成功", rbacManager);
        } else {
            return ResponseEntity.FAIL;
        }
    }

    @RequestMapping("/401")
    @CrossOrigin
    public ResponseEntity response401() {

        return new ResponseEntity(401, "认证异常！", null);
    }
}
